/**
 * Created by shao-yikun on 2017/8/24.
 */
var gUserLoginResult;
var index = "<li class='treeview'>" +
    "<a href='javascript:' onclick=\"$.loadURL('首页', '首页', 'index.html', 0, 0, 0, 0, 0)\">" +
    "<i class='fa fa-dashboard text-primary'></i>" +
    "<span class='text-primary'> 首页</span></a></li>";
var systemStr = "";
var noticeStr = "";
var signboardStr = index;
var roadlineStr = index;
var guardrailStr = index;
var coverStr = index;
var signalmachineStr = index;
var signlampStr = index;
var monitorStr = index;
var solarenergyStr = index;
var highLeverStr = index;
var userStr = "";
var countStr = "";
var alarm = "";

$(function () {
    if (!sessionStorage.rdsUserLoginResult)
        window.location.replace("../login.html");

    gUserLoginResult = JSON.parse(sessionStorage.rdsUserLoginResult);

    if (gUserLoginResult.userIcon == null || gUserLoginResult.userIcon == "")
        if (gUserLoginResult.userSex == "男")
            $("#userStr").attr('src', src = "../images/default_male.png");
        else
            $("#userStr").attr('src', src = "../images/default_female.png");
    else
        $("#userStr").attr('src', src = gUserLoginResult.userIcon);

    $.getIsReadNotice();
    $.getMenuByIds(gUserLoginResult.userPower);

    var html = "";
    if (gUserLoginResult.userActors == 1) {
        html += "<button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
            "onclick='$.updataAbout()'><i class='fa fa-plus-square'></i> 编辑</button>";
    }
    $("#aboutButton").append(html);

    var aboutInfo = $.getAboutInfo();
    $("#showAboutInfo").append(aboutInfo);

    $(".myButton").click(function () {
        var str = $(this).attr('id');
        var tree = "";
        switch (str) {
            case "systemStr":
                sessionStorage.outType = 10;
                tree += systemStr;
                break;
            case "noticeStr":
                sessionStorage.outType = 11;
                tree += noticeStr;
                break;
            case "signboardStr":
                sessionStorage.outType = 0;
                tree += signboardStr + countStr;
                break;
            case "roadlineStr":
                sessionStorage.outType = 6;
                tree += roadlineStr + countStr;
                break;
            case "guardrailStr":
                sessionStorage.outType = 7;
                tree += guardrailStr + countStr;
                break;
            case "coverStr":
                sessionStorage.outType = 9;
                tree += coverStr + countStr;
                break;
            case "signalmachineStr":
                sessionStorage.outType = 3;
                tree += signalmachineStr + countStr;
                break;
            case "signlampStr":
                sessionStorage.outType = 2;
                tree += signlampStr + countStr;
                break;
            case "monitorStr":
                sessionStorage.outType = 8;
                tree += monitorStr + countStr;
                break;
            case "solarenergyStr":
                sessionStorage.outType = 4;
                tree += solarenergyStr + countStr;
                break;
            case "highLeverStr":
                sessionStorage.outType = 5;
                tree += highLeverStr + countStr;
                break;
            case "userStr":
                sessionStorage.outType = 12;
                tree += userStr;
                break;
            case "countStr":
                sessionStorage.outType = 1;
                tree += countStr + "<li><a href='javascript:void(0);' " +
                    "onclick=\"$.loadURL('设备管理','综合搜索','count/colligateSearch.html',)\"><i class='fa fa-wpforms' aria-hidden='true'></i><span>综合搜索</span></a></li>" +
                    "<li><a href='javascript:void(0);'onclick=\"$.loadURL('设备管理','故障综合搜索','count/faultColligateSearch.html',)\"><i class='fa fa-wpforms' aria-hidden='true'></i><span>故障综合搜索</span></a></li>";
                break;
            case "alarm":
                sessionStorage.outType = 13;
                tree += alarm;
                break;
        }

        sessionStorage.rdsTree = tree;
        if (tree != "")
            window.location.href = "workbench.html";
        else
            $.alert("无权限", 2);
    });

    $("#about").click(function () {
        $.showAboutModal();
    });

    $("#appDownload, #appBox").hover(function () {
        $("#appBox").show();
    }, function () {
        $("#appBox").hide();
    });

    if ($(window).height() - 770 > 40)
        $("#footer").height($(window).height() - 770);
});

$.getAboutInfo = function () {
    var result = "";
    $.ajax({
        url: "../rds_feedback/getAboutInfo",
        type: "post",
        async: false,
        data: {
            infoId: 1
        },
        success: function (msg) {
            if (msg) {
                if (msg.isSuccess) {
                    var jsonData = msg.jsonData;
                    $("#aboutTele").val(jsonData[0].orgTele)
                    $("#aboutFax").val(jsonData[0].orgFax)
                    $("#aboutMail").val(jsonData[0].orgMail)
                    $("#aboutWebsite").val(jsonData[0].orgWebsite)
                    result += "<p>电话：" + jsonData[0].orgTele + "</p>" + "<p>传真：" + jsonData[0].orgFax + "</p>" + "<p>邮箱：" + jsonData[0].orgMail + "</p>" +
                        "<p>网址：" + jsonData[0].orgWebsite + "</p>"
                }
            }
        }
    })
    return result;
}

$.updataAbout = function () {


    $("#modal-alarm").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
};

$("#updataAboutInfo").click(function () {

    var aboutTele = $("#aboutTele").val();
    var aboutFax = $("#aboutFax").val();
    var aboutMail = $("#aboutMail").val();
    var aboutWebsite = $("#aboutWebsite").val();
    console.log(aboutWebsite);
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定要修改数据吗？ 请确保每条数据准确无误</b>',
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: "../rds_feedback/updataAboutInfo",
                    type: "POST",
                    data: {
                        infoId: 1,
                        aboutTele: aboutTele,
                        aboutFax: aboutFax,
                        aboutMail: aboutMail,
                        aboutWebsite: aboutWebsite
                    },
                    success: function (msg) {
                        if (msg) {
                            if (msg.isSuccess && msg.jsonData >= 1) {
                                $.alert("修改成功", 1);
                                var html="<p>电话：" + aboutTele + "</p>" + "<p>传真：" + aboutFax + "</p>" + "<p>邮箱：" + aboutMail + "</p>" +
                                    "<p>网址：" + aboutWebsite + "</p>"
                                $("#showAboutInfo").html(html);
                                $("#modal-alarm").modal("hide");
                                $("#showAboutModal").modal("hide");
                            }
                            else
                                $.alert("修改失败", 2);
                        } else
                            $.alert("修改错误", 2);
                    }
                })
            }
        }

    })
})

$.showAboutModal = function () {
    $("#showAboutModal").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};


$.logoutSystem = function () {
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定退出系统?</b>',
        callback: function (result) {
            if (result) {
                sessionStorage.clear();
                window.location.replace("../login.html");
            }
        }
    });
};

$.getMenuByIds = function (menuIds) {
    $.ajax({
        url: '../rds_menu/getMenuByIds',
        type: 'post',
        data: {
            'menuIds': menuIds,
            'userId': gUserLoginResult.userId,
            'orgId': gUserLoginResult.orgId,
            'orgType': gUserLoginResult.orgType
        },
        dataType: 'json',
        success: function (msg) {
            if (msg.jsonData != null && msg.jsonData.length != 0) {
                if (msg.isSuccess == true) {
                    var menuData = msg.jsonData;
                    $.each(menuData, function (i, d1) {
                        if (menuData[i].menuType == 1) {
                            $.each(menuData, function (j, d2) {
                                if (menuData[j].menuFather == menuData[i].menuId) {
                                    switch (menuData[j].menuId) {
                                        case 1001:
                                        case 1002:
                                        case 1003:
                                        case 1004:
                                        case 1005:
                                        case 1006:
                                            systemStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 2001:
                                        case 2002:
                                        case 2003:
                                            noticeStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3001:
                                        case 3002:
                                        case 4001:
                                        case 4002:
                                        case 4003:
                                        case 5001:
                                        case 5002:
                                        case 5003:
                                            signboardStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3014:
                                        case 3015:
                                        case 4019:
                                        case 4020:
                                        case 4021:
                                        case 5020:
                                        case 5021:
                                            roadlineStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3016:
                                        case 3017:
                                        case 4022:
                                        case 4023:
                                        case 4024:
                                        case 5023:
                                        case 5024:
                                            guardrailStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3012:
                                        case 3013:
                                        case 4016:
                                        case 4017:
                                        case 4018:
                                        case 5017:
                                        case 5018:
                                            coverStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3006://信号机
                                        case 3007:
                                        case 4007:
                                        case 4008:
                                        case 4009:
                                        case 5008:
                                        case 5009:
                                        case 5030:
                                            signalmachineStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3008://信号灯
                                        case 3009:
                                        case 4010:
                                        case 4011:
                                        case 4012:
                                        case 5011:
                                        case 5012:
                                        case 5029:
                                            signlampStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3010://监控箱
                                        case 3011:
                                        case 4013:
                                        case 4014:
                                        case 4015:
                                        case 5014:
                                        case 5015:
                                        case 5032:
                                            monitorStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3004://太阳能
                                        case 3005:
                                        case 4004:
                                        case 4005:
                                        case 4006:
                                        case 5005:
                                        case 5006:
                                        case 5031:
                                            solarenergyStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3020://限高杆
                                        case 3021:
                                        case 4025:
                                        case 4026:
                                        case 4027:
                                        case 5026:
                                        case 5027:
                                        case 5028:
                                            highLeverStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3003:
                                            countStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 6001:
                                            userStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 6002:
                                            userStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.modifyUserPwd()\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 6003:
                                            userStr += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.feedBack()\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                        case 3019:
                                        case 3018:
                                            alarm += "<li><a href='javascript:void(0);' " +
                                                "onclick=\"$.loadURL('" + menuData[i].menuName + "','" +
                                                menuData[j].menuName + "','" +
                                                menuData[j].menuUrl + "','" +
                                                menuData[j].powerInsert + "','" +
                                                menuData[j].powerDelete + "','" +
                                                menuData[j].powerUpdate + "','" +
                                                menuData[j].powerSelect + "','" +
                                                menuData[j].powerCheck + "')\">" +
                                                menuData[j].menuIcon + " <span>" + menuData[j].menuName +
                                                "</span></a></li>";
                                            break;
                                    }
                                }
                            });

                        }
                    });
                }
            }
        }
    });
};

$.getIsReadNotice = function () {
    $.ajax({
        url: '../rds_noticeApp/getIsReadNotice',
        type: 'post',
        data: {
            'userId': gUserLoginResult.userId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg)
                if (msg.isSuccess == true) {
                    var isReadCount = msg.jsonData;
                    if (isReadCount != 0) {
                        if (isReadCount > 9) {
                            $("#plus").show();
                            $("#isReadCount").text(9);
                        } else {
                            $("#plus").hide();
                            $("#isReadCount").text(isReadCount);
                        }
                        $("#countSpan").show();
                    } else {
                        $("#countSpan").hide();
                    }
                }
                else
                    $.alert('获取个数失败', 2);
            else
                $.alert('系统错误', 2);
        },
        error: function () {
            $.alert('系统错误', 2);
        }
    })
};

$.alert = function (alertContent, alertType) {
    $("#alertDiv").remove();
    $("body").append("<div id='alertDiv' style='position:fixed;top:100px;width:24%;margin-left:38%;z-index:1500;display:none;'><div id='alertContent' style='padding:10px;' class='alert text-center'></div></div>");
    if (alertType == 1) {
        $("#alertContent").html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button><b class='customer-title'><span class='glyphicon glyphicon-ok-sign'></span> " + alertContent + "</b>");
        $("#alertDiv").fadeIn();
        $("#alertContent").addClass("alert-success");
        $("#alertDiv").delay(1500).hide(0, function () {
            $(this).remove();
        });
    } else {
        $("#alertContent").html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button><b class='customer-title'><span class='glyphicon glyphicon-info-sign'></span> " + alertContent + "</b>");
        $("#alertDiv").fadeIn();
        $("#alertContent").addClass("alert-danger");
        $("#alertDiv").delay(5000).hide(0, function () {
            $(this).remove();
        });
    }
    $("#alertDiv button").click(function () {
        onclick = $("#alertDiv").fadeOut();
    })
};